package com.example.myapplication.old.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.old.entity.User1;

import java.util.List;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder> {
    private List<User1> list;
    private Context context;
    private UserOnclickListener userOnclickListener;

    public void setData(List<User1> list){
        this.list=list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact1,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        User1 user1 = list.get(position);
        holder.imageView.setImageResource(user1.getImg());
        holder.textView.setText(user1.getUsername());
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userOnclickListener != null){
                    userOnclickListener.onUserClick(user1,position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        //控件初始化
        TextView textView;
        ImageView imageView;
        View item;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.username);
            imageView = itemView.findViewById(R.id.img);
            item = itemView.findViewById(R.id.item_user);
        }
    }
    public void setUserOnclickListener(UserOnclickListener onclickListener){
        this.userOnclickListener = onclickListener;
    }
    public interface UserOnclickListener{
        void onUserClick(User1 user1 , int position);
    }
}
